/*
 * @Author: long
 * @Date: 2021-04-23 11:47:43
 * @LastEditTime: 2021-11-03 11:24:19
 * @LastEditors: Please set LastEditors
 * @Description: 全局功能块封装
 * @FilePath: \ant-design-vue-jeecge:\hjimi\人脸辨识云\html\gitlab\pc\faceCloudWebsite\src\utils\index.js
 */

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

export function ops() {
  let a = 9
  return {
    ops1(x) {
      alert(a + x)
    }
  }
}

/**
 * @description： 获取自定义时间段
 * @param {*}
 * @return {*}
 */

export function timeStremHandle() {
  let now = new Date() //当前日期
  let nowDayOfWeek = now.getDay() //今天本周的第几天
  let nowDay = now.getDate() //当前日
  let nowMonth = now.getMonth() //当前月
  let nowYear = now.getYear() //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0 //

  let lastMonthDate = new Date() //上月日期
  lastMonthDate.setDate(1)
  lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
  let lastYear = lastMonthDate.getYear()
  let lastMonth = lastMonthDate.getMonth()

  return {
//格局化日期：yyyy-MM-dd
    formatDate(date) {
      var myyear = date.getFullYear()
      var mymonth = date.getMonth() + 1
      var myweekday = date.getDate()

      if (mymonth < 10) {
        mymonth = "0" + mymonth
      }
      if (myweekday < 10) {
        myweekday = "0" + myweekday
      }
      return (myyear + "-" + mymonth + "-" + myweekday)
    },

//获得某月的天数
    getMonthDays(myMonth) {
      var monthStartDate = new Date(nowYear, myMonth, 1)
      var monthEndDate = new Date(nowYear, myMonth + 1, 1)
      var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
      return days
    },

//获得本季度的开端月份
    getQuarterStartMonth() {
      var quarterStartMonth = 0
      if (nowMonth < 3) {
        quarterStartMonth = 0
      }
      if (2 < nowMonth && nowMonth < 6) {
        quarterStartMonth = 3
      }
      if (5 < nowMonth && nowMonth < 9) {
        quarterStartMonth = 6
      }
      if (nowMonth > 8) {
        quarterStartMonth = 9
      }
      return quarterStartMonth
    },

//获得本周的开端日期
    getWeekStartDate() {
      var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
      return formatDate(weekStartDate)
    },

//获得本周的停止日期
    getWeekEndDate() {
      var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
      return formatDate(weekEndDate)
    },

//获得本月的开端日期
    getMonthStartDate() {
      var monthStartDate = new Date(nowYear, nowMonth, 1)
      return formatDate(monthStartDate)
    },

//获得本月的停止日期
    getMonthEndDate() {
      var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth))
      return formatDate(monthEndDate)
    },

//获得上月开端时候
    getLastMonthStartDate() {
      var lastMonthStartDate = new Date(nowYear, lastMonth, 1)
      return formatDate(lastMonthStartDate)
    },

//获得上月停止时候
    getLastMonthEndDate() {
      var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth))
      return formatDate(lastMonthEndDate)
    },

//获得本季度的开端日期
    getQuarterStartDate() {

      var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1)
      return formatDate(quarterStartDate)
    },

//或的本季度的停止日期
    getQuarterEndDate() {
      var quarterEndMonth = getQuarterStartMonth() + 2
      var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth))
      return formatDate(quarterStartDate)
    },
    
//日期减
    DateDiff(sDate1, sDate2) {
      var aDate, oDate1, oDate2, iDays
      aDate = sDate1.split("-")
      oDate1 = new Date(aDate[0], aDate[1] - 1, aDate[2])
      aDate = sDate2.split("-")
      oDate2 = new Date(aDate[0], aDate[1] - 1, aDate[2])
      iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)
      if ((oDate1 - oDate2) < 0) {
        return -iDays
      }
      return iDays
    },

// 最近一个月年月日
 latelyTimeStrem() {
  var strem = new Date(now)
  strem.setDate(now.getDate() - 30)
  return {
    startDay: this.formatDate(new Date(nowYear, nowMonth, nowDay -30)),
    nowDay: this.formatDate(new Date(nowYear, nowMonth, nowDay))
  }
 }
  }
}

/**
 * @description: 开始日期+天数=结束日期
 * @param {String}
 */
export function getEndDay(dateTemp, days) {
  dateTemp = dateTemp.split("-")
  var nDate = new Date(dateTemp[1] + "-" + dateTemp[2] + "-" + dateTemp[0])
  var millSeconds = Math.abs(nDate) + days * 24 * 60 * 60 * 1000
  var rDate = new Date(millSeconds)
  var year = rDate.getFullYear()
  var month = rDate.getMonth() + 1
  if (month < 10) month = "0" + month
  var date = rDate.getDate()
  if (date < 10) date = "0" + date
  return year + "-" + month + "-" + date
}

/**
 * @description: 开始日期+月数=结束日期
 * @param {String}
 */

 export function getEndMonth(dtstr, n) {
     var s = dtstr.split("-")
     var yy = parseInt(s[0])
     var mm = parseInt(s[1])
     var dd = parseInt(s[2])
     var dt = new Date(yy, mm, dd)
     
     var num=dt.getMonth() + parseInt(n)
     if(num/12>1){
        yy+=Math.floor(num/12)
        mm=num%12
     }else{
         mm+=parseInt(n)
     }
     return yy + "-" + mm  + "-" + dd
 }

/**
 * @description: 开始日期+年数=结束日期
 * @param {String}
 */
export function getEndYear(date, years) {
  var now = new Date(date);
  var intYear = now.getFullYear() + parseInt(years);
  var intMonth = now.getMonth() + 1; //正常的月份，
  var intDay = now.getDate() - 1; //日期-1
  if (intDay == 0) {
  intMonth--; //减少一个月
  if (intMonth == 0) {
  intYear--; //0:减少一年
  intMonth = 12;
  intDay = 31;
  }
  else if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) {
  intDay = 30; //4,6,9,11:30天
  }
  else if (intMonth == 2) {
  intDay = 28; //2:28/29
  if (intYear % 4 == 0) {
  intDay = 29;
  }
  } else {
  intDay = 31; //1,3,5,7,8,10,12 :31天
  }
  }
  var strMonth = (intMonth) < 10 ? "0" + (intMonth).toString() : (intMonth).toString();
  var strDay = (intDay) < 10 ? "0" + (intDay).toString() : (intDay).toString();
  var strEndDate = intYear + "-" + strMonth + "-" + strDay;
  return strEndDate;
  }


  /**
   * @param {string} url
   * @returns {Object}
   */
  export function getQueryObject(url) {
    url = url == null ? window.location.href : url
    const search = url.substring(url.lastIndexOf('?') + 1)
    const obj = {}
    const reg = /([^?&=]+)=([^?&=]*)/g
    search.replace(reg, (rs, $1, $2) => {
      const name = decodeURIComponent($1)
      let val = decodeURIComponent($2)
      val = String(val)
      obj[name] = val
      return rs
    })
    return obj
  }

  /**
   * @param {string} input value
   * @returns {number} output value
   */
  export function byteLength(str) {
    // returns the byte length of an utf8 string
    let s = str.length
    for (var i = str.length - 1; i >= 0; i--) {
      const code = str.charCodeAt(i)
      if (code > 0x7f && code <= 0x7ff) s++
      else if (code > 0x7ff && code <= 0xffff) s += 2
      if (code >= 0xDC00 && code <= 0xDFFF) i--
    }
    return s
  }

  /**
   * @param {Array} actual
   * @returns {Array}
   */
  export function cleanArray(actual) {
    const newArray = []
    for (let i = 0; i < actual.length; i++) {
      if (actual[i]) {
        newArray.push(actual[i])
      }
    }
    return newArray
  }

  /**
   * @param {Object} json
   * @returns {Array}
   */
  export function param(json) {
    if (!json) return ''
    return cleanArray(
      Object.keys(json).map(key => {
        if (json[key] === undefined) return ''
        return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
      })
    ).join('&')
  }

  /**
   * @param {string} url
   * @returns {Object}
   */
  export function param2Obj(url) {
    const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
    if (!search) {
      return {}
    }
    const obj = {}
    const searchArr = search.split('&')
    searchArr.forEach(v => {
      const index = v.indexOf('=')
      if (index !== -1) {
        const name = v.substring(0, index)
        const val = v.substring(index + 1, v.length)
        obj[name] = val
      }
    })
    return obj
  }

  /**
   * @param {string} val
   * @returns {string}
   */
  export function html2Text(val) {
    const div = document.createElement('div')
    div.innerHTML = val
    return div.textContent || div.innerText
  }

  /**
   * Merges two objects, giving the last one precedence
   * @param {Object} target
   * @param {(Object|Array)} source
   * @returns {Object}
   */
  export function objectMerge(target, source) {
    if (typeof target !== 'object') {
      target = {}
    }
    if (Array.isArray(source)) {
      return source.slice()
    }
    Object.keys(source).forEach(property => {
      const sourceProperty = source[property]
      if (typeof sourceProperty === 'object') {
        target[property] = objectMerge(target[property], sourceProperty)
      } else {
        target[property] = sourceProperty
      }
    })
    return target
  }

  /**
   * @param {HTMLElement} element
   * @param {string} className
   */
  export function toggleClass(element, className) {
    if (!element || !className) {
      return
    }
    let classString = element.className
    const nameIndex = classString.indexOf(className)
    if (nameIndex === -1) {
      classString += '' + className
    } else {
      classString =
        classString.substr(0, nameIndex) +
        classString.substr(nameIndex + className.length)
    }
    element.className = classString
  }

  /**
   * @param {string} type
   * @returns {Date}
   */
  export function getTime(type) {
    if (type === 'start') {
      return new Date().getTime() - 3600 * 1000 * 24 * 90
    } else {
      return new Date(new Date().toDateString())
    }
  }

  /**
   * @param {Function} func
   * @param {number} wait
   * @param {boolean} immediate
   * @return {*}
   */
  export function debounce(func, wait, immediate) {
    let timeout, args, context, timestamp, result

    const later = function () {
      // 据上一次触发时间间隔
      const last = +new Date() - timestamp

      // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
      if (last < wait && last > 0) {
        timeout = setTimeout(later, wait - last)
      } else {
        timeout = null
        // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
        if (!immediate) {
          result = func.apply(context, args)
          if (!timeout) context = args = null
        }
      }
    }

    return function (...args) {
      context = this
      timestamp = +new Date()
      const callNow = immediate && !timeout
      // 如果延时不存在，重新设定延时
      if (!timeout) timeout = setTimeout(later, wait)
      if (callNow) {
        result = func.apply(context, args)
        context = args = null
      }

      return result
    }
  }

  /**
   * 判值法
   * 数组或对象深拷贝
   * @param {Object || Array} Obejct
   */
  export function deepClone(source) {
    if (!source && typeof source !== 'object') {
      throw new Error('error arguments', 'deepClone')
    }
    const targetObj = source.constructor === Array ? [] : {}
    Object.keys(source).forEach(keys => {
      if (source[keys] && typeof source[keys] === 'object') {
        targetObj[keys] = deepClone(source[keys])
      } else {
        targetObj[keys] = source[keys]
      }
    })
    return targetObj
  }

  /** 递归法
   * 数组或对象深拷贝
   * @param {Object || Array} Obejct
   */
  export function deepCopy(obj) {
    var newobj = obj.constructor === Array ? [] : {}
    if (typeof obj !== 'object') {
      return
    }
    for (var i in obj) {
      newobj[i] = typeof obj[i] === 'object' ? deepCopy(obj[i]) : obj[i];
    }
    return newobj
  }

  /**
   * @param {Array} arr
   * @returns {Array}
   */
  export function uniqueArr(arr) {
    return Array.from(new Set(arr))
  }

  /**
   * @returns {string}
   */
  export function createUniqueString() {
    const timestamp = +new Date() + ''
    const randomNum = parseInt((1 + Math.random()) * 65536) + ''
    return (+(randomNum + timestamp)).toString(32)
  }

  /**
   * Check if an element has a class
   * @param {HTMLElement} elm
   * @param {string} cls
   * @returns {boolean}
   */
  export function hasClass(ele, cls) {
    return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
  }

  /**
   * Add class to element
   * @param {HTMLElement} elm
   * @param {string} cls
   */
  export function addClass(ele, cls) {
    if (!hasClass(ele, cls)) ele.className += ' ' + cls
  }

  /**
   * Remove class from element
   * @param {HTMLElement} elm
   * @param {string} cls
   */
  export function removeClass(ele, cls) {
    if (hasClass(ele, cls)) {
      const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
      ele.className = ele.className.replace(reg, ' ')
    }
  }

  /**
  * @description: 文件下载
  */
  class DownFile {

    // 此方法只针对浏览器不能解析的文件进行下载，若是带后缀名的图片和pdf等不支持
    parse_file(url, fileName) {
      if ('download' in document.createElement('a')) {
        let link = document.createElement('a')
        link.setAttribute('download', `${fileName}`);
        link.style.display = 'none'
        link.href = `${process.env.VUE_APP_BASE_API}${url}`
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
      } else {
        navigator.msSaveBlob(blob, fileName)
      }
    }

    //  支持所有文件下载(单文件)
    nslookup_file(url) {
      let domForm = document.createElement('form'),
        npt = document.createElement('input')
      domForm.appendChild(npt)
      // npt.setAttribute('name', 'imageId')
      // npt.setAttribute('value', value)
      domForm.setAttribute('method', 'get')
      domForm.setAttribute('action', `${process.env.VUE_APP_BASE_API}${url}`)
      domForm.style.display = 'none'
      document.body.appendChild(domForm)
      domForm.submit()
      document.body.removeChild(domForm)
    }

    // 支持所有图片下载
    downloadIamge(imgsrc, name, isUrl) {
      let getUrl = isUrl ?? false
      let image = new Image();
      // 解决跨域 Canvas 污染问题
      image.setAttribute("crossOrigin", "anonymous")
      image.onload = function () {
        let canvas = document.createElement("canvas")
        canvas.width = image.width
        canvas.height = image.height
        let context = canvas.getContext("2d")
        context.drawImage(image, 0, 0, image.width, image.height)
        let url = canvas.toDataURL("image/png"), //得到图片的base64编码数据
          a = document.createElement("a"),
          event = new MouseEvent("click")
        a.download = name || "photo"
        a.href = url
        a.dispatchEvent(event)
      };
      if(getUrl) {
        image.src = `${process.env.VUE_APP_BASE_API}${imgsrc}`
      } else {
        image.src = imgsrc
      }
    }

    // 将文本信息下载为文件
    downloadTxt(content, filename) {
      var link = document.createElement('a');
      link.download = filename;
      link.style.display = 'none';
      var blob = new Blob([content]);
      link.href = URL.createObjectURL(blob);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }
  export const DOWNFILE = new DownFile()


  /**
  * @description: 获取系统时分秒
  */
  export function getSystemTime() {
    let timer = null
    setInterval(function () {
      let time = new Date()
      let hour = checkTime(time.getHours())
      let minite = checkTime(time.getMinutes())
      let second = checkTime(time.getSeconds())
      function checkTime(i) {
        if (i < 10) return "0" + i
        return i
      }
      timer = hour + ":" + minite + ":" + second
      return timer
    }, 1000)
  }

  /**
  * @description: 信息转为base64
  */
  export function encodes(str) {
    var Base64 = {
      // private property
      _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

      // public method for encoding
      encode: function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

          chr1 = input.charCodeAt(i++);
          chr2 = input.charCodeAt(i++);
          chr3 = input.charCodeAt(i++);

          enc1 = chr1 >> 2;
          enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
          enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
          enc4 = chr3 & 63;

          if (isNaN(chr2)) {
            enc3 = enc4 = 64;
          } else if (isNaN(chr3)) {
            enc4 = 64;
          }

          output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
      },

      // public method for decoding
      decode: function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

          enc1 = this._keyStr.indexOf(input.charAt(i++));
          enc2 = this._keyStr.indexOf(input.charAt(i++));
          enc3 = this._keyStr.indexOf(input.charAt(i++));
          enc4 = this._keyStr.indexOf(input.charAt(i++));

          chr1 = (enc1 << 2) | (enc2 >> 4);
          chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
          chr3 = ((enc3 & 3) << 6) | enc4;

          output = output + String.fromCharCode(chr1);

          if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
          }
          if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
          }

        }

        output = Base64._utf8_decode(output);

        return output;

      },

      // private method for UTF-8 encoding
      _utf8_encode: function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

          var c = string.charCodeAt(n);

          if (c < 128) {
            utftext += String.fromCharCode(c);
          } else if ((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
          } else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
          }

        }

        return utftext;
      },

      // private method for UTF-8 decoding
      _utf8_decode: function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

          c = utftext.charCodeAt(i);

          if (c < 128) {
            string += String.fromCharCode(c);
            i++;
          } else if ((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i + 1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
          } else {
            c2 = utftext.charCodeAt(i + 1);
            c3 = utftext.charCodeAt(i + 2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
          }

        }

        return string;
      }

    }
    return Base64.encode(str)
  }

  /**
  * @description 按需格式化 年月日分秒
  * @param {obj, ...string}
  * @returns {Date}
  */
  export function getDates(obj, rawDate, createTimeFrom, createTimeTo, switchParam, onece) {
    if (onece === 2) {
      rawDate && rawDate.length
        ? (obj[createTimeFrom] = moment(rawDate[0]).format(`YYYY-MM-DD${switchParam}`), (obj[createTimeTo] = moment(rawDate[1]).format(`YYYY-MM-DD${switchParam}`)))
        : obj[createTimeFrom] = obj[createTimeTo] = null
    } else if (onece === 1) {
      obj[createTimeFrom] = moment(rawDate[0]).format(`YYYY-MM-DD${switchParam}`)
    }
  }

  /**
  * @description 复制文本信息
  * @param {txt}
  * @returns {Date}
  */
  export function copyTxtHandle(txt, isMsg, tipFun) {
    let isM = isMsg ?? false
    try {
      let npt = document.createElement('input')
      npt.value = txt
      document.body.appendChild(npt)
      npt.select()
      document.execCommand("Copy")
      document.body.removeChild(npt)
      if (isM) {
        // Message.success('复制成功')
        tipFun()
      }
    } catch {
      document.body.removeChild(npt)
    }
  }